package cn.iocoder.yudao.module.ai.service.operationalarm;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.operationalarm.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.operationalarm.OperationAlarmDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.operationalarm.OperationAlarmMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 运营中心-报警信息 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class OperationAlarmServiceImpl implements OperationAlarmService {

    @Resource
    private OperationAlarmMapper operationAlarmMapper;

    @Override
    public Long createOperationAlarm(OperationAlarmSaveReqVO createReqVO) {
        // 插入
        OperationAlarmDO operationAlarm = BeanUtils.toBean(createReqVO, OperationAlarmDO.class);
        operationAlarmMapper.insert(operationAlarm);
        // 返回
        return operationAlarm.getId();
    }

    @Override
    public void updateOperationAlarm(OperationAlarmSaveReqVO updateReqVO) {
        // 校验存在
        validateOperationAlarmExists(updateReqVO.getId());
        // 更新
        OperationAlarmDO updateObj = BeanUtils.toBean(updateReqVO, OperationAlarmDO.class);
        operationAlarmMapper.updateById(updateObj);
    }

    @Override
    public void deleteOperationAlarm(Long id) {
        // 校验存在
        validateOperationAlarmExists(id);
        // 删除
        operationAlarmMapper.deleteById(id);
    }

    private void validateOperationAlarmExists(Long id) {
        if (operationAlarmMapper.selectById(id) == null) {
            throw exception(OPERATION_ALARM_NOT_EXISTS);
        }
    }

    @Override
    public OperationAlarmDO getOperationAlarm(Long id) {
        return operationAlarmMapper.selectById(id);
    }

    @Override
    public PageResult<OperationAlarmDO> getOperationAlarmPage(OperationAlarmPageReqVO pageReqVO) {
        return operationAlarmMapper.selectPage(pageReqVO);
    }

}